package wsz.sharding.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import wsz.sharding.dao.OrderRepo;
import wsz.sharding.entity.Order;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wsz
 * @desc：
 * @date 2022/6/7
 */
@Service
public class OrderService {
    @Autowired
    private OrderRepo orderRepo;

    /**
     * 数据插入
     */
    public void doInsert(){
        for (int i=0; i < 2; i++) {
            Order order = new Order();
            order.setName("log" + i);
            order.setYear(2022 + i);
            order.setTime(getDate(i));
            orderRepo.save(order);
        }
    }

    public List<Order> getAll(int year) {
        Order search = new Order();
        search.setYear(year);
        return orderRepo.findAll(Example.of(search));
    }

    private Date getDate(int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, offset);
        return calendar.getTime();
    }

    public List<Order> findByParam(int year, String name) {
        return orderRepo.findByParam(year, name);
    }

    public List<Map> findByParams(int year, String name) {
        return orderRepo.findByParams(year, name);
    }

}
